﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.驾驶员管理
{
    public partial class Repeater : System.Web.UI.Page
    {
        //调用车队信息
        TruckTeamBLL truckteam = new TruckTeamBLL();
        //调用驾驶员信息
        DriverBLL driver = new DriverBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadioButtonList();
                BindDriver();
            }
        }
        /// <summary>
        /// 绑定车队信息
        /// </summary>
        private void RadioButtonList()
        {
            DataTable table = truckteam.GetTruckList();
            ddl_teamname.DataSource = table;
            ddl_teamname.DataTextField = "TeamName";
            ddl_teamname.DataValueField = "TeamID";
            ddl_teamname.DataBind();
            ddl_teamname.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定驾驶员信息数据
        /// </summary>
        private void BindDriver()
        {
            //姓名
            string name = txt_name.Text.Trim();
            //性别
            string sex = rbl_sex.SelectedValue;
            //身份证号码
            string number = txt_idcard.Text.Trim();
            //生日
            DateTime? brith_in = null;
            if (!string.IsNullOrWhiteSpace(txt_birth_in.Text.Trim()))
            {
                brith_in = Convert.ToDateTime(txt_birth_in.Text);
            }
            //生日至
            DateTime? brith_end = null;
            if (!string.IsNullOrWhiteSpace(txt_birth_end.Text.Trim()))
            {
                brith_end = Convert.ToDateTime(txt_birth_end.Text);
            }
            //电话
            string phone = txt_phone.Text.Trim();
            //聘用日期
            DateTime? date_in = null;
            if (!string.IsNullOrWhiteSpace(txt_checkintime_in.Text.Trim()))
            {
                date_in = Convert.ToDateTime(txt_checkintime_in.Text);
            }
            //聘用日期至
            DateTime? date_end = null;
            if (!string.IsNullOrWhiteSpace(txt_checkintime_end.Text.Trim()))
            {
                date_end = Convert.ToDateTime(txt_checkintime_end.Text);
            }

            DriverMODEL model = new DriverMODEL()
            {
                //姓名
                Name = name,
                //性别
                Sex = Convert.ToInt32(sex),
                //身份证
                Number = number,
                //生日
                Brith_in = brith_in,
                //生日至
                Brith_end = brith_end,
                //电话
                Phone = phone,
                //聘用日期
                CheckInTime_in = date_in,
                //聘用日期至
                CheckInTime_end = date_end,
            };
            List<DriverQueryModel> list = driver.Getdrivrmodel(model);
            repe_Driver.DataSource = list;
            repe_Driver.DataBind();
        }
        protected void btn_search_Click(object sender, EventArgs e)
        {
            //驾驶员信息的绑定事件方法
            BindDriver();
        }

        protected void btn_reset_Click(object sender, EventArgs e)
        {
            txt_name.Text = null;
            txt_idcard.Text = "";
            txt_birth_end.Text = "";
            txt_birth_in.Text = "";
            txt_checkintime_end.Text = "";
            txt_checkintime_in.Text = "";
            txt_phone.Text = "";
            rbl_bindingstate.SelectedValue = null;
            rbl_sex.SelectedValue = null;
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            //跳转添加驾驶员信息页面
            Response.Redirect("AddDriver.aspx");
        }

        protected void repe_Driver_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int driverid = Convert.ToInt32(e.CommandArgument);
                int result = driver.Getdrive_delete(driverid);
                if (result == 1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！')</script>");
                    BindDriver();
                }
                else if (result == -1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('该信息已删除，请勿重复删除！')</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert(''删除失败！)</script>");
                }
            }
        }

        protected void lb_Bind_Click(object sender, EventArgs e)
        {
            int driverid = Convert.ToInt32(((LinkButton)sender).CommandArgument);
            Response.Redirect("DriverBindCar.aspx?id="+driverid);
        }

        /// <summary>
        /// 车辆绑定
        /// </summary>
        /// <param name="driverid"></param>
        /// <returns></returns>
        protected bool GetBindState(int driverid)
        {
            return driver.GetBindCount(driverid) > 0 ? false : true;
        }

        protected bool GetUnBindState(int driverid, int state)
        {
            bool Truck = false;
            int truckCount = driver.GetBindCount(driverid);
            if (truckCount > 0)
            {
                Truck = true;
            }
            if (Truck && state == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}